package org.example.springbootwebexample.controller;


import lombok.extern.slf4j.Slf4j;
import org.example.springbootwebexample.pojo.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
public class SessionController {


    //设置 Cookie
    @GetMapping("/c1")
    public Result c1(HttpServletResponse response){
        log.info("响应Cookie  /c1");
        response.addCookie(new Cookie("username", "jinyong"));

        return Result.success();
    }

    @GetMapping("/c2")
    public Result c2(HttpServletRequest request){
        log.info("响应Cookie  /c2");
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie :cookies){
            if(cookie.getName().equals("username")){
                System.out.printf("username" + cookie.getValue());
            }
        }

        return Result.success();
    }

    //获取 session
    @GetMapping("/s1")
    public Result s1(HttpSession session){
        log.info("请求  /s1");
        session.setAttribute("session_username", "huangpu01");
        session.setAttribute("session_flag", "666");

        System.out.println(session.hashCode());
        return Result.success();
    }

    @GetMapping("/s2")
    public Result s2(HttpServletRequest request){
        log.info("响应  /s2");
        HttpSession session = request.getSession();
        Object session_username = session.getAttribute("session_username");
        Object session_flag = session.getAttribute("session_flag");
        log.info("{}    {}", session_username, session_flag);

        System.out.println(session.hashCode());
        return Result.success();
    }


}
